import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';

import 'calendar_page_viewModel.dart';
import 'calendar_widget.dart';
import 'toast_widget.dart';

/*
* Location 标记当前选中日期和之前的日期相比，
* left： 是在之前日期之前
* mid：  和之前日期相等
* right：在之前日期之后
* */
enum Location { left, mid, right }

typedef void SelectDateOnTap(DayModel checkInTimeModel, DayModel leaveTimeModel);

class CalendarPage extends StatefulWidget {
    final DayModel startTimeModel; // 外部传入的开始日期
    final DayModel endTimeModel; // 外部传入的结束日期
    final SelectDateOnTap selectDateOnTap; // 确定按钮的callback 给外部传值
    CalendarPage({this.startTimeModel, this.endTimeModel, this.selectDateOnTap});
    
    @override
    _CalendarPageState createState() => _CalendarPageState();
}

class _CalendarPageState extends State<CalendarPage> {
    
    String _selectCheckInTime = '选择开始时间';
    String _selectLeaveTime = '选择结束时间';
    bool _isSelectCheckInTime = false; // 是否选择开始日期
    bool _isSelectLeaveTime = false; // 是否选择结束日期
    int _checkInDays = 0; // 选择天数
    
    // 保存当前选中的开始日期和结束日期
    DayModel _selectCheckInTimeModel = null;
    DayModel _isSelectLeaveTimeModel = null;
    
    List<CalendarItemViewModel> _list = [];
    
    @override
    void initState() {
        super.initState();
        // 加载日历数据源
        _list = CalendarViewModel().getItemList();
        print(_list.length);
        // 处理外部传入的选中日期
        if (widget.startTimeModel != null && widget.endTimeModel != null) {
            for (int i = 0; i < _list.length; i++) {
                CalendarItemViewModel model = _list[i];
                if (model.month == widget.startTimeModel.month) {
                    _updateDataSource(widget.startTimeModel.year, widget.startTimeModel.month, widget.startTimeModel.dayNum);
                }
                if (model.month == widget.endTimeModel.month) {
                    _updateDataSource(widget.endTimeModel.year, widget.endTimeModel.month, widget.endTimeModel.dayNum);
                }
            }
        }
    }
    
    @override
    Widget build(BuildContext context) {
        final data = MediaQuery.of(context);
        // 屏幕宽高
        final screenHeight = data.size.height;
        final screenWidth = data.size.width;
        return Container(
            height: screenHeight - 80,
            child: ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                    CalendarItemViewModel itemModel = _list[index];
                    return CalendarItem((year, month, checkInTime) {
                            _updateCheckInLeaveTime(year, month, checkInTime);
                        },
                        itemModel,
                    );
                },
                itemCount: _list.length,
            ),
        );
    }
    
    /*
  * 比较后面的日期是比model日期小（left） 还是相等(mid) 还是大 (right)
  * */
    _compareTime(DayModel model, int year, int month, int day) {
        if (year > model.year) {
            return Location.right;
        } else if (year == model.year) {
            if (model.month < month) {
                return Location.right;
            } else if (model.month == month) {
                if (model.dayNum < day) {
                    return Location.right;
                } else if (model.dayNum == day) {
                    return Location.mid;
                } else {
                    return Location.left;
                }
            } else {
                return Location.right;
            }
        } else {
            return Location.left;
        }
    }
    
    /*
  * 更新日历的数据源
  * */
    _updateDataSource(int year, int month, int checkInTime) {
        // 左右指针 用来记录选择的开始日期和结束日期
        DayModel firstModel;
        DayModel lastModel;
        
        for (int i = 0; i < _list.length; i++) {
            CalendarItemViewModel model = _list[i];
            if (model.firstSelectModel != null) {
                firstModel = model.firstSelectModel;
            }
            if (model.lastSelectModel != null) {
                lastModel = model.lastSelectModel;
            }
        }
        
        if (firstModel != null && lastModel != null) {
            for (int i = 0; i < _list.length; i++) {
                CalendarItemViewModel model = _list[i];
                model.firstSelectModel = null;
                model.lastSelectModel = null;
                firstModel = null;
                lastModel = null;
                for (int i = 0; i < model.list.length; i++) {
                    DayModel dayModel = model.list[i];
                    dayModel.isSelect = false;
                    if (_compareTime(dayModel, year, month, checkInTime) == Location.mid) {
                        dayModel.isSelect = true;
                        model.firstSelectModel = dayModel;
                        _isSelectCheckInTime = true;
                        _isSelectLeaveTime = false;
                        _selectCheckInTime = '$year.$month.$checkInTime';
                        _selectCheckInTimeModel = dayModel;
                    }
                }
            }
            _checkInDays = 0;
            _isSelectLeaveTime = false;
            _selectLeaveTime = '选择结束时间';
            _isSelectLeaveTimeModel = null;
        } else if (firstModel != null && lastModel == null) {
            if (_compareTime(firstModel, year, month, checkInTime) == Location.left) {
                for (int i = 0; i < _list.length; i++) {
                    CalendarItemViewModel model = _list[i];
                    model.firstSelectModel = null;
                    model.lastSelectModel = null;
                    firstModel = null;
                    lastModel = null;
                    for (int i = 0; i < model.list.length; i++) {
                        DayModel dayModel = model.list[i];
                        dayModel.isSelect = false;
                        if (_compareTime(dayModel, year, month, checkInTime) == Location.mid) {
                            dayModel.isSelect = !dayModel.isSelect;
                            model.firstSelectModel = dayModel;
                            _isSelectCheckInTime = dayModel.isSelect ? true : false;
                            _selectCheckInTime = '$year.$month.$checkInTime';
                            _selectCheckInTimeModel = dayModel;
                        }
                    }
                }
                _checkInDays = 0;
                _isSelectLeaveTime = false;
                _selectLeaveTime = '选择结束时间';
                _isSelectLeaveTimeModel = null;
            } else if (_compareTime(firstModel, year, month, checkInTime) == Location.mid) { //点击了自己
                for (int i = 0; i < _list.length; i++) {
                    CalendarItemViewModel model = _list[i];
                    model.lastSelectModel = null;
                    if (model.month == month) {
                        for (int i = 0; i < model.list.length; i++) {
                            DayModel dayModel = model.list[i];
                            if (_compareTime(dayModel, year, month, checkInTime) == Location.mid) {
                                dayModel.isSelect = !dayModel.isSelect;
                                model.firstSelectModel = dayModel.isSelect ? dayModel : null;
                                _selectCheckInTimeModel = dayModel.isSelect ? dayModel : null;
                                _isSelectCheckInTime = dayModel.isSelect ? true : false;
                                _selectCheckInTime = dayModel.isSelect ? '$year.$month.$checkInTime' : '选择开始时间';
                            }
                        }
                    }
                }
                _checkInDays = 0;
                _isSelectLeaveTime = false;
                _selectLeaveTime = '选择结束时间';
                _isSelectLeaveTimeModel = null;
            } else if (_compareTime(firstModel, year, month, checkInTime) == Location.right) {
                if (month == firstModel.month) {
                    // 统计选择天数
                    int _calculateDays = 1;
                    for (int i = 0; i < _list.length; i++) {
                        CalendarItemViewModel model = _list[i];
                        if (model.month == month) {
                            for (int i = 0; i < model.list.length; i++) {
                                DayModel dayModel = model.list[i];
                                if (dayModel.dayNum == checkInTime) {
                                    dayModel.isSelect = true;
                                    model.lastSelectModel = dayModel;
                                    _isSelectLeaveTimeModel = dayModel;
                                    _isSelectLeaveTime = true;
                                    _selectLeaveTime = '$year.$month.$checkInTime';
                                } else if (dayModel.dayNum > firstModel.dayNum && dayModel.dayNum < checkInTime) {
                                    dayModel.isSelect = true;
                                    _calculateDays++;
                                }
                            }
                        }
                    }
                    _checkInDays = _calculateDays;
                } else {
                    // 统计选择天数
                    int _calculateDays = 1;
                    for (int i = 0; i < _list.length; i++) {
                        CalendarItemViewModel model = _list[i];
                        if (model.month == firstModel.month) {
                            for (int i = 0; i < model.list.length; i++) {
                                DayModel dayModel = model.list[i];
                                if (dayModel.dayNum > firstModel.dayNum) {
                                    dayModel.isSelect = true;
                                    _calculateDays++;
                                }
                            }
                        } else if (model.month > firstModel.month && model.month < month) {
                            for (int i = 0; i < model.list.length; i++) {
                                DayModel dayModel = model.list[i];
                                dayModel.isSelect = true;
                                _calculateDays++;
                            }
                        } else if (month == model.month) {
                            for (int i = 0; i < model.list.length; i++) {
                                DayModel dayModel = model.list[i];
                                if (dayModel.dayNum < checkInTime) {
                                    dayModel.isSelect = true;
                                    _calculateDays++;
                                } else if (dayModel.dayNum == checkInTime) {
                                    dayModel.isSelect = true;
                                    model.lastSelectModel = dayModel;
                                    _isSelectLeaveTimeModel = dayModel;
                                    _isSelectLeaveTime = true;
                                    _selectLeaveTime = '$year.$month.$checkInTime';
                                }
                            }
                        }
                    }
                    _checkInDays = _calculateDays;
                }
            }
        } else if (firstModel == null && lastModel == null) {
            for (int i = 0; i < _list.length; i++) {
                CalendarItemViewModel model = _list[i];
                model.firstSelectModel = null;
                model.lastSelectModel = null;
                firstModel = null;
                lastModel = null;
                for (int i = 0; i < model.list.length; i++) {
                    DayModel dayModel = model.list[i];
                    dayModel.isSelect = false;
                    if (_compareTime(dayModel, year, month, checkInTime) == Location.mid) {
                        dayModel.isSelect = true;
                        model.firstSelectModel = dayModel;
                        _isSelectCheckInTime = true;
                        _selectCheckInTimeModel = dayModel;
                        _isSelectLeaveTime = false;
                        _selectCheckInTime = '$year.$month.$checkInTime';
                    }
                }
            }
        }
    }
    
    /*
  * 点击日期的回调事件
  * */
    _updateCheckInLeaveTime(int year, int month, int checkInTime) {
        print(year);
        print(month);
        print(checkInTime);
        // 更新数据源
        _updateDataSource(year, month, checkInTime);
        // 刷新UI
        setState(() {});
    }
}
